<?php

namespace Miniapp\Controllers;

class PayController extends AuthController {

    /**
     * 用户打赏
     */
    public function createOrderAction() {
        $objectType = $_POST['objectType'];
        $objectId = $_POST['objectId'];
        $cash = $_POST['cash'];
        $payService = service("Pay/PayService");
        //创建订单
        $result = $payService->createPayOrder($this->userId, $objectType, $objectId, $cash, $this->user);
        //调用统一下单
        $resulst2 = lib("WxPay")->unifiedorder($this->user['openId1'], $result['orderNo'], round($cash, 2) * 100);
        //生成小程序需要的paySign
        $resulst3 = lib("WxPay")->createPaySign($resulst2);
        $result = array_merge($result, $resulst3);
        $this->renderJson($result);
    }

    /**
     * 发红包记录
     */
    public function sendListAction() {
        $p = intval($_POST['p']) ? intval($_POST['p']) : 1;
        $psize = intval($_POST['psize']) ? intval($_POST['psize']) : 10;
        $payService = service("Pay/PayService");
        $result['list'] = $payService->getMySendRedpacketList($this->userId, $p, $psize);
        $result['minisendMoney'] = $this->user['minisendMoney'];
        $result['minisendCount'] = $this->user['minisendCount'];
        $this->renderJson($result);
    }

    /**
     * 提现
     */
    public function withdrawMoneyAction() {
        $money = $_POST['money'];
        $payService = service("Pay/PayService");
        $result = $payService->withdrawMoney($this->userId, $money);
        $this->renderJson(true);
    }

    /**
     * 微信订单回调信息
     */
    public function wxPayOrderCallBackAction() {
        $wxpay = lib("WxPay");
        $resultXml = $wxpay->verifyNotify();
        $dom = new \DOMDocument();
        $dom->loadXML($resultXml);
        $return_code = $dom->getElementsByTagName("return_code")->item(0)->nodeValue;
        $this->logger->write_log("充值接口已经进入回调:" . $return_code, "info");
        if ($return_code == "SUCCESS") {
            try {
                $post = file_get_contents("php://input");
                $this->redisCache->set("wxpost", $post);
                $dom->loadXML($post);
                $out_trade_no = $dom->getElementsByTagName("out_trade_no")->item(0)->nodeValue;
                $total_fee = $dom->getElementsByTagName("total_fee")->item(0)->nodeValue;
                $openid = $dom->getElementsByTagName("openid")->item(0)->nodeValue;
                $payOrdeInfo = service("Pay/PayService")->getPayOrderByOrderNo($out_trade_no);
                if (($total_fee / 100) == $payOrdeInfo['cash']) {
                    service("Pay/PayService")->payOrderSuccess($payOrdeInfo['id'], $openid);
                } else {
                    $this->logger->write_log("订单号:" . $out_trade_no . "支付的金额不对,妄想骗我们的金币", "error");
                }
            } catch (\Exception $exc) {
                $this->logger->write_log($exc->getMessage(), "error");
            }
        }
        echo $resultXml;
        exit();
    }

    /**
     * 微信订单回调信息
     */
    public function wxRewardPayOrderCallBackAction() {
        $wxpay = lib("WxPay");
        $resultXml = $wxpay->verifyNotify();
        $dom = new \DOMDocument();
        $dom->loadXML($resultXml);
        $return_code = $dom->getElementsByTagName("return_code")->item(0)->nodeValue;
        $this->logger->write_log("充值接口已经进入回调:" . $return_code, "info");
        if ($return_code == "SUCCESS") {
            try {
                $post = file_get_contents("php://input");
                $this->redisCache->set("wxpost", $post);
                $dom->loadXML($post);
                $out_trade_no = $dom->getElementsByTagName("out_trade_no")->item(0)->nodeValue;
                $total_fee = $dom->getElementsByTagName("total_fee")->item(0)->nodeValue;
                $openid = $dom->getElementsByTagName("openid")->item(0)->nodeValue;
                $payOrdeInfo = service("Pay/RewardPayService")->getPayOrderByOrderNo($out_trade_no);
                if (($total_fee / 100) == $payOrdeInfo['cash']) {
                    service("Pay/RewardPayService")->payOrderSuccess($payOrdeInfo['id'], $openid);
                } else {
                    $this->logger->write_log("订单号:" . $out_trade_no . "支付的金额不对,妄想骗我们的金币", "error");
                }
            } catch (\Exception $exc) {
                $this->logger->write_log($exc->getMessage(), "error");
            }
        }
        echo $resultXml;
        exit();
    }

}
